<script setup>
import { ref, onMounted } from "vue";
import { get } from "@/request/index.js";
import TnSwiper from "@tuniao/tnui-vue3-uniapp/components/swiper/src/swiper.vue";

onMounted(() => {
	fetchData();
	banner();
	rightswiper();
});

//十个导航选项的地址
function fetchData() {
	get("default/index", {
		success: (res) => { },
	})
		.then((response) => {
			const data = response.data.data;
			navIconlist.value = data.nav_icon_list;
		})
		.catch((err) => { });
}

// 轮播图数据
const currentSwiperIndex = ref(0);
const swiperData = [
	"https://apphoshna.c8plus.net/web/uploads/image/store_1_0/4df88908849c1bacb2d0f557fcf599dee428d926.jpg",
	,
];
const navIconlist = ref({});
// 中间的广告栏、因此找不到API地址我用了链接方式
const adList = [
	"https://apphoshna.c8plus.net/web/uploads/image/store_1_0/189a0aa1834c6121b74495fb02f9947900aeaa17.gif",
];

function banner() {
	get("group/index/index", {
		success: (res) => { },
	})
		.then((response) => {
			const data = response.data.data;
			coverSwiperdata.value = data.banner;
		})
		.catch((err) => { });
}

const coverSwiperdata = ref({});

const fruits = [
	{
		name: "果品水果",
		title: "果品水果",
		image: "/static/image/moon.png",
	},
	{
		name: "新鲜蔬菜",
		title: "新鲜蔬菜",
		image: "/static/image/moon.png",
	},
	{
		name: "粮油调料",
		title: "粮油调料",
		image: "/static/image/moon.png",
	},
	{
		name: "新鲜肉类",
		title: "新鲜肉类",
		image: "/static/image/moon.png",
	},
];
const selectedFruit = ref(-1);

function selectFruit(index) {
	selectedFruit.value = selectedFruit.value === index ? -1 : index;
}
// 左边的轮播图
function rightswiper() {
	get("group/index/good-list", {
		success: (res) => { },
	})
		.then((response) => {
			const data = response.data.data;
			const group1 = [data.list[0], data.list[1]];
			const group2 = [data.list[2], data.list[3]];
			rightswiperlist.value = [group1, group2];
		})
		.catch((err) => { });
}

const rightswiperlist = ref({});
</script>
<template>
	<view class="app">
		<!-- swiper -->
		<view class="SwiperBox">
			<TnSwiper v-model="currentSwiperIndex" :data="swiperData" height="344" width="694" indicator>
				<template #default="{ data }">
					<view class="swiper-data">
						<image :src="data" class="image" mode="aspectFill" />
					</view>
				</template>
			</TnSwiper>
		</view>
		<view class="selectBox">
			<template v-for="(item, index) in navIconlist" :key="index">
				<view class="selectbox">
					<image :src="item.pic_url" class="img" />
					<view class="name">
						{{ item.name }}
					</view>
				</view>
			</template>
		</view>
		<view class="gifimg">
			<image :src="adList" />
		</view>
		<view class="hengxian">
			<view class="left">
				<view class="leftone"></view>
				<view class="leftt"></view>
				<view class="leftth"></view>
			</view>
			<view class="cont">每日火爆团购</view>
			<view class="left">
				<view class="leftth"></view>
				<view class="leftt"></view>
				<view class="leftone"></view>
			</view>
		</view>
		<view class="tswiperBox">
			<view class="twoswiper">
				<TnSwiper :data="coverSwiperdata" height="450" width="360" autoplay loop interval="3000">
					<template #default="{ data }">
						<view class="swiper-data">
							<image :src="data.pic_url" class="image" mode="aspectFill" />
						</view>
					</template>
				</TnSwiper>
			</view>
			<view class="goodsswiper">
				<swiper class="swiper" :indicator-dots="false" :autoplay="true" :interval="3000">
					<swiper-item class="swiperitem" v-for="(item, index) in rightswiperlist" :key="index">
						<view class="itop">
							<view class="name">{{ item[0].name }}</view>
							<view class="bottomBox">
								<view class="Price">
									<view class="price">
										低至<text style="color: red">{{ item[0].price }}</text>元起
									</view>
									<view class="pricebtn">立即购买</view>
								</view>
								<view class="coverbox">
									<image class="cover" :src="item[0].cover_pic"></image>
								</view>
							</view>
						</view>
						<view class="ibottom">
							<view>{{ item[1].name }}</view>
							<view class="bottomBox">
								<view class="Price">
									<view class="price">
										低至<text style="color: red">{{ item[1].price }}</text>元起
									</view>
									<view class="pricebtn">立即购买</view>
								</view>
								<view class="coverbox">
									<image class="cover" :src="item[1].cover_pic"></image>
								</view>
							</view>
						</view>
					</swiper-item>
				</swiper>
			</view>
		</view>
		<view>
			<view class="hengxian">
				<view class="left">
					<view class="leftone"></view>
					<view class="leftt"></view>
					<view class="leftth"></view>
				</view>
				<view class="cont">本社区火爆商品</view>
				<view class="left">
					<view class="leftth"></view>
					<view class="leftt"></view>
					<view class="leftone"></view>
				</view>
			</view>
		</view>
		<view class="comsh">
			<view class="comtitle" v-for="(item, index) in fruits" :key="index" @click="selectFruit(index)"
				:class="{ active: selectedFruit === index }">
				<view :style="{
					color: selectedFruit === index ? '#ff1929' : '#333',
					fontSize: selectedFruit === index ? '34rpx' : '32rpx',
				}">
					{{ item.name }}
				</view>
				<view class="fruit-name" :style="{
					color: selectedFruit === index ? '#ff1929' : '#999',
					fontSize: selectedFruit === index ? '24rpx' : '24rpx',
				}">
					{{ item.title }}
				</view>
				<image class="cimg" v-if="selectedFruit === index" :style="{
					width: selectedFruit === index ? '60rpx' : '',
					height: selectedFruit === index ? '24rpx' : '',
				}" :src="item.image" />
			</view>
		</view>
		<view class="hengxian">
			<view class="left">
				<view class="leftone"></view>
				<view class="leftt"></view>
				<view class="leftth"></view>
			</view>
			<view class="cont">团长推出新品团购</view>
			<view class="left">
				<view class="leftth"></view>
				<view class="leftt"></view>
				<view class="leftone"></view>
			</view>
		</view>
		<view class="hengxian">
			<view class="left">
				<view class="leftone"></view>
				<view class="leftt"></view>
				<view class="leftth"></view>
			</view>
			<view class="cont">大品牌推出商品</view>
			<view class="left">
				<view class="leftth"></view>
				<view class="leftt"></view>
				<view class="leftone"></view>
			</view>
		</view>
	</view>
</template>
<style lang="scss" scoped>
.app {
	.SwiperBox {
		padding: 28rpx;

		.swiper-data {
			width: 100%;
			height: 100%;
			border-radius: 20rpx;

			.image {
				width: 100%;
				height: 100%;
				border-radius: inherit;
			}
		}
	}

	.selectBox {
		width: 100%;
		height: 360rpx;
		background: #fff;
		border-radius: 10rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: center;

		.selectbox {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			.img {
				width: 100rpx;
				height: 100rpx;
				margin: 25rpx;
			}

			.name {
				margin-top: -20rpx;
				font-size: 14px;
				color: #333;
			}
		}
	}

	.gifimg {
		margin-top: 20rpx;
		width: 100%;
		height: 170rpx;
		background: #fff;
		border-radius: 10rpx;
		overflow: hidden;
	}

	.hengxian {
		margin-top: 10rpx;
		height: 48rpx;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;

		.left {
			display: flex;
			justify-content: center;
			align-items: center;

			.leftone {
				margin: 0 6rpx 0 0;
				height: 16rpx;
				width: 8rpx;
				background: #fa4f516e;
			}

			.leftt {
				margin: 0 6rpx 0 0;
				height: 24rpx;
				width: 8rpx;
				background: #fa4f51bb;
			}

			.leftth {
				margin: 0 6rpx 0 0;
				width: 8rpx;
				height: 32rpx;
				background: #fa4f51eb;
			}
		}

		.cont {
			font-size: 27rpx;
			color: #212121;
			padding: 0 12rpx;
		}
	}

	.tswiperBox {
		display: flex;

		.twoswiper {
			margin: 10rpx;

			.swiper-data {
				width: 100%;
				height: 100%;
				border-radius: 20rpx;

				.image {
					width: 100%;
					height: 100%;
					border-radius: inherit;
				}
			}
		}

		.goodsswiper {
			padding: 0 20rpx;
			margin: 10rpx;
			height: 450rpx;
			width: 350rpx;
			border-radius: 15rpx;
			background: #fff;

			.swiper {
				height: 100%;
				width: 100%;

				.swiperitem {
					.itop {
						margin: 0 30rpx 30rpx 0;
						width: 169px;
						height: 107px;
					}

					.ibottom {
						margin: 0 30rpx 30rpx 0;
						width: 169px;
						height: 107px;
					}

					.name {
						margin: 30rpx 0 0;
						color: #555;
						font-size: 26rpx;
					}

					.bottomBox {
						width: 338rpx;
						height: 110rpx;
						display: flex;

						.Price {
							width: 77px;
							height: 55px;

							.pricebtn {
								margin-top: 20rpx;
								width: 130rpx;
								height: 50rpx;
								color: #fafafa;
								font-size: 22rpx;
								border-radius: 14rpx;
								text-align: center;
								line-height: 50rpx;
								background: linear-gradient(258.6deg,
										rgba(251, 124, 29, 1) 5.66%,
										rgba(254, 0, 125, 1) 89.84%);
							}

							.price {
								font-size: 22rpx;
							}
						}

						.coverbox {
							width: 76px;
							height: 55px;
							display: flex;
							justify-content: center;
							align-items: center;

							.cover {
								width: 100%;
								height: 100%;
								border-radius: 24rpx;
							}
						}
					}
				}
			}
		}
	}

	.comsh {
		padding: 10rpx;
		width: 100%;
		height: 130rpx;
		background-color: #fff;
		display: flex;
		justify-content: space-around;

		.comtitle {
			color: #000;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
		}
	}
}
</style>
